package acs.cat;

import java.text.MessageFormat;
import java.util.Date;

import acs.cat.message.MessageProducer;
import acs.cat.message.Transaction;
import acs.cat.message.internal.DefaultMessageProducer;

public class Cat {
	private static volatile boolean s_init = false;
	private static Cat s_instance = new Cat();
	private MessageProducer m_producer;
	
	public static Transaction newTransaction(String type, String name) {
		return Cat.getProducer().newTransaction(type, name);
	}

	public static MessageProducer getProducer() {
		checkAndInitialize();

		return s_instance.m_producer;
	}
	private static void checkAndInitialize() {
		if (!s_init) {
			synchronized (s_instance) {
				if (!s_init) {
//					initialize(new File(getCatHome(), "client.xml"));
					initialize();
					log("WARN", "Cat is lazy initialized!");
					s_init = true;
				}
			}
		}
	}
	
	public static void initialize(String... servers) {
		s_instance.m_producer=new DefaultMessageProducer();
	}
	
	static void log(String severity, String message) {
		MessageFormat format = new MessageFormat("[{0,date,MM-dd HH:mm:ss.sss}] [{1}] [{2}] {3}");

		System.out.println(format.format(new Object[] { new Date(), severity, "cat", message }));
	}
}
